package hedron.view;

import static hedron.i18n.Internationalization._;
import hedron.Activator;
import hedron.Application;
import hedron.db.model.Contact;
import hedron.dialog.ContactsDialog;
import hedron.dialog.ContactsTypeDialog;
import hedron.dialog.StoreNameDialog;
import hedron.dialog.StringDialog;
import hedron.editor.ContactsEditor;
import hedron.editor.InvoiceEditor;
import hedron.editor.StockEditor;
import hedron.editor.StoreEditor;
import hedron.editorinput.DummyEditorInput;
import java.util.HashMap;
import java.util.logging.Level;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

//IWorkbenchPage page = getSite().getPage();
// page =
// PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
// else if in ViewPart // page = getSite ().getPage ();

public class MenuView extends ViewPart {
	public static final String ID = "HEDRON.MenuView";

	private Listener menuListener;
	private HashMap<TreeItem, Listener> menuActions = new HashMap<TreeItem, Listener>(5);
	private ExpandBar bar;

	private static int democounter = 4;

	@Override
	public void createPartControl(Composite parent) {

		this.setPartName(_("View.Menu.Title"));
		this.setTitleImage(Activator.getImage("icons/hedron_icon.png"));

		menuListener = new Listener() {
			public void handleEvent(Event event) {
				Point point = new Point(event.x, event.y);
				TreeItem item = ((Tree)event.widget).getItem(point);
				if (item != null) {
					Listener listener = menuActions.get(item);
					if (listener != null) {
						listener.handleEvent(event);
					}
				}
			}
		};
		
		bar = new ExpandBar(parent, SWT.V_SCROLL);

		// Module section
		{
			Composite composite = new Composite(bar, SWT.NONE);
			GridLayout layout = new GridLayout();
			layout.horizontalSpacing = 0;
			layout.marginHeight = 0;
			layout.marginWidth = 0;
			composite.setLayout(layout);

			{
				Tree tree = new Tree(composite, SWT.V_SCROLL | SWT.BORDER);
				tree.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
				tree.addListener(SWT.MouseDoubleClick, menuListener);

				{
					final String title = _("Menu.Modules.Contacts");
					TreeItem item = new TreeItem(tree, 0);
					item.setText(title);
					item.setImage(Activator.getImage("icons/contacts.png"));
					menuActions.put(item, new Listener() {
						public void handleEvent(Event event) {
							IWorkbenchPage page = getSite().getPage();
							try {
								page.openEditor(new DummyEditorInput(title, title), ContactsEditor.ID);
							} catch (PartInitException e) {
								Application.log.log(Level.OFF, "", e);
							}
						}
					});
				}
				{
					final String title = _("Menu.Modules.Stocks");
					TreeItem item = new TreeItem(tree, 0);
					item.setText(title);
					item.setImage(Activator.getImage("icons/stock.png"));
					menuActions.put(item, new Listener() {
						public void handleEvent(Event event) {
							IWorkbenchPage page = getSite().getPage();
							try {
								page.openEditor(new DummyEditorInput(title, title), StockEditor.ID);
							} catch (PartInitException e) {
								Application.log.log(Level.OFF, "", e);
							}
						}
					});
				}
				{
					final String title = _("Menu.Modules.Stores");
					TreeItem item = new TreeItem(tree, 0);
					item.setText(title);
					item.setImage(Activator.getImage("icons/store.png"));
					menuActions.put(item, new Listener() {
						public void handleEvent(Event event) {
							IWorkbenchPage page = getSite().getPage();
							try {
								page.openEditor(new DummyEditorInput(title, title), StoreEditor.ID);
							} catch (PartInitException e) {
								Application.log.log(Level.OFF, "", e);
							}
						}
					});
				}
				{
					final String title = _("Menu.Modules.Invoice");
					TreeItem item = new TreeItem(tree, 0);
					item.setText(title);
					item.setImage(Activator.getImage("icons/invoice.png"));
					menuActions.put(item, new Listener() {
						public void handleEvent(Event event) {
							IWorkbenchPage page = getSite().getPage();
							try {
								page.openEditor(new DummyEditorInput(Application.ID.getUID(), title), InvoiceEditor.ID);
							} catch (PartInitException e) {
								Application.log.log(Level.OFF, "", e);
							}
						}
					});
				}
			}

			ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 0);
			item0.setText(_("Menu.Modules.Title"));
			item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
			item0.setControl(composite);
			item0.setImage(Activator.getImage("icons/hedron_menu.png"));

			item0.setExpanded(true);
		}

		// Administration Section
		{
			Composite composite = new Composite(bar, SWT.NONE);
			GridLayout layout = new GridLayout();
			layout.horizontalSpacing = 0;
			layout.marginHeight = 0;
			layout.marginWidth = 0;
			composite.setLayout(layout);

			{
				Tree tree = new Tree(composite, SWT.V_SCROLL | SWT.BORDER);
				tree.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
				tree.addListener(SWT.MouseDoubleClick, menuListener);
				{
					TreeItem item = new TreeItem(tree, 0);
					item.setText(_("Menu.Administration.CompanyData"));
					item.setImage(Activator.getImage("icons/contacts.png"));
					menuActions.put(item, new Listener() {
						public void handleEvent(Event event) {
							Contact companyInfo = Application.CONTACTS.getCompanyContacts();
							if (companyInfo != null) {
								ContactsDialog dialog = new ContactsDialog(Application.getActiveWorkbench().getShell(),
										companyInfo);
								dialog.open();
							} else {
								final String errorMsg = "Cannot load Company Info";
								Application.log.log(Level.SEVERE, errorMsg);
								Application.setStatusMessage(2, errorMsg);
							}
						}
					});
				}
				{
					TreeItem item = new TreeItem(tree, 0);
					item.setText(_("Menu.Administration.ContactTypes"));
					item.setImage(Activator.getImage("icons/contacts_red.png"));
					menuActions.put(item, new Listener() {
						public void handleEvent(Event event) {
							ContactsTypeDialog dialog = new ContactsTypeDialog(
									Application.getActiveWorkbench().getShell());
							dialog.open();
						}
					});
				}
				{
					TreeItem item = new TreeItem(tree, 0);
					item.setText(_("Menu.Administration.Stores"));
					item.setImage(Activator.getImage("icons/store.png"));
					menuActions.put(item, new Listener() {
						public void handleEvent(Event event) {
							StoreNameDialog dialog = new StoreNameDialog(
									Application.getActiveWorkbench().getShell());
							dialog.open();
						}
					});
				}
			}

			ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 1);
			item0.setText(_("Menu.Administration.Title"));
			item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
			item0.setControl(composite);
			item0.setImage(Activator.getImage("icons/settings_menu.png"));

			item0.setExpanded(false);
		}

		// Test Section
		{
			Composite composite = new Composite(bar, SWT.NONE);
			GridLayout layout = new GridLayout();
			layout.horizontalSpacing = 0;
			layout.marginHeight = 0;
			layout.marginWidth = 0;
			composite.setLayout(layout);

			{
				Tree tree = new Tree(composite, SWT.V_SCROLL | SWT.BORDER);
				tree.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
				tree.addListener(SWT.MouseDoubleClick, menuListener);

				{
					TreeItem item = new TreeItem(tree, 0);
					item.setText("String Dialog");
					item.setImage(Activator.getImage("icons/dir.png"));
					menuActions.put(item, new Listener() {
						public void handleEvent(Event event) {
							StringDialog dialog = new StringDialog(Application.getActiveWorkbench().getShell(),
									"enter text", "default text");
							dialog.create();
							if (dialog.open() == Window.OK) {

							}
						}
					});
				}
			}

			ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 2);
			item0.setText("test elements");
			item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
			item0.setControl(composite);
			item0.setImage(Activator.getImage("icons/hedron_menu.png"));

			item0.setExpanded(false);
		}
		bar.setSpacing(4);
	}

	@Override
	public void setFocus() {
		bar.setFocus();
	}
}
